
#include "stdafx.h"
#include "coolsbcustomdraw.h"
#include <windows.h>
#include <commctrl.h>
#include "coolscroll.h"

class CDrawTable
{
public:
	int x;
	int y;
	int width;
	int height;
};

CDrawTable cdt_bar[] = 
{
	{ 0, 0, 12, 12 },
};

CDrawTable cdt_thumb[] = 
{
	{ 0,  0, 12, 24 },	
	{ 12, 0, 12, 24 },
	{ 24, 0, 12, 24 },
};

CDrawTable cdt_button[] = 
{
	{ 0,  0,  12, 13 },
	{ 0,  13, 12, 13 },

	{ 12, 0,  12, 13 },
	{ 12, 13, 12, 13 },
	
	{ 24, 0,  12, 13 },
	{ 24, 13, 12, 13 },
};

void CCoolSBCustomDraw::InitDC(HBITMAP bar_image, HBITMAP thumb_image, HBITMAP buttons_image)
{
	LONG dwDataLen = 0;
	LPBYTE lpData = NULL;

	m_hdcSBBar	= CreateCompatibleDC(NULL);
	m_hBmpBar = bar_image;
	SelectObject(m_hdcSBBar, m_hBmpBar);

	m_hdcSBThumb	= CreateCompatibleDC(NULL);
	m_hBmpThumb = thumb_image;
	SelectObject(m_hdcSBThumb, m_hBmpThumb);

	m_hdcSBButton	= CreateCompatibleDC(NULL);
	m_hBmpButton = buttons_image;
	SelectObject(m_hdcSBButton, m_hBmpButton);

	CalcDrawTables();

	m_fCustomDraw = TRUE;
}

void CCoolSBCustomDraw::DeinitDC()
{
// 	DeleteObject(hBmpBar);
// 	DeleteObject(hBmpThumb);
// 	DeleteObject(hBmpButton);

	if (m_hdcSBBar)
	{
		DeleteDC(m_hdcSBBar);
		m_hdcSBBar = NULL;
	}

	if (m_hdcSBThumb)
	{
		DeleteDC(m_hdcSBThumb);
		m_hdcSBThumb = NULL;
	}

	if (m_hdcSBButton)
	{
		DeleteDC(m_hdcSBButton);
		m_hdcSBButton = NULL;
	}

	m_fCustomDraw = FALSE;
}

void CCoolSBCustomDraw::CalcDrawTables()
{
	BITMAP bm;

	::GetObject(m_hBmpBar, sizeof(BITMAP), &bm);
	CDrawTable l_cdt_bar[] = { { 0, 0, bm.bmWidth, bm.bmHeight } };
	memcpy(cdt_bar, l_cdt_bar, sizeof(cdt_bar));

	::GetObject(m_hBmpThumb, sizeof(BITMAP), &bm);
	bm.bmWidth /= 3;
	CDrawTable l_cdt_thumb[] = 
	{
		{ 0,  0, bm.bmWidth, bm.bmHeight },	
		{ bm.bmWidth, 0, bm.bmWidth, bm.bmHeight },
		{ bm.bmWidth * 2, 0, bm.bmWidth, bm.bmHeight },
	};
	memcpy(cdt_thumb, l_cdt_thumb, sizeof(cdt_thumb));

	::GetObject(m_hBmpButton, sizeof(BITMAP), &bm);
	bm.bmWidth /= 3;
	bm.bmHeight /= 2;
	CDrawTable l_cdt_button[] = 
	{
		{ 0,  0,  bm.bmWidth, bm.bmHeight },
		{ 0,  bm.bmHeight, bm.bmWidth, bm.bmHeight },

		{ bm.bmWidth, 0,  bm.bmWidth, bm.bmHeight },
		{ bm.bmWidth, bm.bmHeight, bm.bmWidth, bm.bmHeight },
		
		{ bm.bmWidth * 2, 0,  bm.bmWidth, bm.bmHeight },
		{ bm.bmWidth * 2, bm.bmHeight, bm.bmWidth, bm.bmHeight },
	};
	memcpy(cdt_button, l_cdt_button, sizeof(cdt_button));
}

LRESULT CCoolSBCustomDraw::HandleCustomDraw(UINT ctrlid, NMCSBCUSTOMDRAW *nm)
{
	RECT *rc;
	UINT code = NM_CUSTOMDRAW;

	UNREFERENCED_PARAMETER(ctrlid);

	if (!m_fCustomDraw)
		return CDRF_DODEFAULT;

	if (nm->dwDrawStage == CDDS_PREPAINT)
	{
		if (m_fCustomDraw)
			return CDRF_SKIPDEFAULT;
		else
			return CDRF_DODEFAULT;
	}

	if (nm->dwDrawStage == CDDS_POSTPAINT)
	{		
	}

	if (nm->nBar == SB_VERT)
	{
		rc = &nm->rect;
		CDrawTable* cdt[4];

		if (nm->uState == CDIS_HOT)		
		{
			cdt[0] = &cdt_bar[0];
			cdt[1] = &cdt_thumb[1];
			cdt[2] = &cdt_button[2];
			cdt[3] = &cdt_button[3];
		}
		else if (nm->uState == CDIS_SELECTED)
		{
			cdt[0] = &cdt_bar[0];
			cdt[1] = &cdt_thumb[2];
			cdt[2] = &cdt_button[4];
			cdt[3] = &cdt_button[5];
		}
		else
		{
			cdt[0] = &cdt_bar[0];
			cdt[1] = &cdt_thumb[0];
			cdt[2] = &cdt_button[0];
			cdt[3] = &cdt_button[1];
		}
		
		switch (nm->uItem)
		{
		case HTSCROLL_UP:
			StretchBlt(nm->hdc, rc->left, rc->top, rc->right - rc->left, rc->bottom - rc->top,
			m_hdcSBButton, cdt[2]->x, cdt[2]->y, cdt[2]->width, cdt[2]->height, SRCCOPY);
			break;

		case HTSCROLL_DOWN:
			StretchBlt(nm->hdc, rc->left, rc->top, rc->right - rc->left, rc->bottom - rc->top,
			m_hdcSBButton, cdt[3]->x, cdt[3]->y, cdt[3]->width, cdt[3]->height, SRCCOPY);
			break;
		
		case HTSCROLL_PAGEGUP:
		case HTSCROLL_PAGEGDOWN:
		case HTSCROLL_NORMAL:
			StretchBlt(nm->hdc, rc->left, rc->top, rc->right - rc->left, rc->bottom - rc->top,
			m_hdcSBBar, cdt[0]->x, cdt[0]->y, cdt[0]->width, cdt[0]->height, SRCCOPY);
			break;
		case HTSCROLL_THUMB:
			{
				int nHalf = cdt[1]->height / 2;
				
				StretchBlt(nm->hdc, rc->left, rc->top, rc->right - rc->left, nHalf, 
					m_hdcSBThumb, cdt[1]->x, cdt[1]->y, cdt[1]->width, nHalf, SRCCOPY);

				StretchBlt(nm->hdc, rc->left, rc->top + nHalf, rc->right - rc->left, (rc->bottom - rc->top) - cdt[1]->height + 1, 
					m_hdcSBThumb, cdt[1]->x, nHalf, cdt[1]->width, 1, SRCCOPY);
				
				StretchBlt(nm->hdc, rc->left, rc->top + (rc->bottom - rc->top) - nHalf, rc->right - rc->left, nHalf, 
					m_hdcSBThumb, cdt[1]->x, nHalf, cdt[1]->width, cdt[1]->height - nHalf, SRCCOPY);

			}
			break;
		}

		return CDRF_SKIPDEFAULT;
	}
	else
	{
		return CDRF_DODEFAULT;
	}
}

